package com.lingkun.demo.sys.controller;


import com.lingkun.demo.sys.entity.SysMenu;
import com.lingkun.demo.sys.entity.sysVo.menuOne;
import com.lingkun.demo.sys.service.SysMenuService;
import com.lingkun.demo.utils.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小龙
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "权限管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;


    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public DataResult editMenu(@RequestBody menuOne menu){
        SysMenu  sysMenu  =  new SysMenu();
    sysMenu.setMenuId(menu.getMenuId()); sysMenu.setPath(menu.getPath());
    sysMenu.setName(menu.getName()); sysMenu.setComponent(menu.getComponent());
    sysMenu.setRedirect(menu.getRedirect()); sysMenu.setHidden(menu.getHidden());
    sysMenu.setAlwaysShow(menu.getAlwaysShow()); sysMenu.setParentId(menu.getParentId());
    sysMenu.setTitle(menu.getMeta().getTitle()); sysMenu.setIcon(menu.getMeta().getIcon());
    sysMenu.setNoCache(menu.getMeta().getNoCache()); sysMenu.setActiveMenu(menu.getPath());
    sysMenu.setBreadcrumb(menu.getMeta().getBreadcrumb()); sysMenu.setActiveMenu(menu.getMeta().getActiveMenu());
        Boolean flag =  sysMenuService.updateById(sysMenu);
         return  flag?DataResult.getResult(20000,"权限修改成功"):
          DataResult.getResult(20001,"权限修改失败");
    }


    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete/{id}/{parent}")
    public DataResult deleteMenu(@PathVariable("id") String id, @PathVariable String parent){
          if("0".equals(parent)){
             List<String>  list  =   sysMenuService.listMenuIds(id);
             list.add(id);
             sysMenuService.removeByIds(list);
            }
          else {
              sysMenuService.removeById(id);
          }
        return DataResult.getResult(20000,"删除菜单成功");
    }

    @ApiOperation(value = "创建新的菜单")
    @PostMapping("/create")
    public DataResult addMenu(@RequestBody menuOne menu){
        SysMenu  sysMenu  =  new SysMenu();
        sysMenu.setPath(menu.getPath());
        sysMenu.setName(menu.getName()); sysMenu.setComponent(menu.getComponent());
        sysMenu.setRedirect(menu.getRedirect()); sysMenu.setHidden(menu.getHidden());
        sysMenu.setAlwaysShow(menu.getAlwaysShow()); sysMenu.setParentId(menu.getParentId());
        sysMenu.setTitle(menu.getMeta().getTitle()); sysMenu.setIcon(menu.getMeta().getIcon());
        sysMenu.setNoCache(menu.getMeta().getNoCache()); sysMenu.setActiveMenu(menu.getPath());
        sysMenu.setBreadcrumb(menu.getMeta().getBreadcrumb()); sysMenu.setActiveMenu(menu.getMeta().getActiveMenu());
        Boolean flag =  sysMenuService.save(sysMenu);
        return  flag?DataResult.getResult(20000,"添加路由成功"):
                DataResult.getResult(20001,"添加路由失败");
    }


}

